﻿using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Admin
{
    public partial class Prepare_data : Form
    {
        int group_count = 90;
        int teach_count = 16;
        int student_count = 262;
        int teacher_choice = 8;
        public Prepare_data()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var res = Sugar_helper.Db.Ado.ExecuteCommand("truncate table Student; truncate table g_data;truncate table t_data;truncate table teacher; truncate table stu_group;");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            group_count = Convert.ToInt32(textBox1.Text);
            teach_count = Convert.ToInt32(textBox2.Text);
            student_count = Convert.ToInt32(textBox4.Text);
            teacher_choice = Convert.ToInt32(textBox3.Text);

            openFileDialog1.Multiselect = true;
            var teacher_file = "";
            var stu_file = "";
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;
            else
            {
                teacher_file = openFileDialog1.FileNames[0];
                stu_file = openFileDialog1.FileNames[1];
            }

            var res = Sugar_helper.Db.Ado.ExecuteCommand("truncate table Student; truncate table g_data;truncate table t_data;truncate table teacher; truncate table stu_group;");

            import_teacher(teacher_file);
            import_stu(stu_file);


            string sql = "";

            List<int> ints;
            ints = GetRandomNumbers(1, student_count, group_count);

            for (int i = 0; i < ints.Count; i++)
            {
                Models.Stu_group stu_Group = new Models.Stu_group();
                stu_Group.group_name = (i+1).ToString();
                stu_Group.group_lead = ints[i];
                stu_Group.members = 1;
                stu_Group.project_info = (i + 1).ToString();
                stu_Group.project_name = (i + 1).ToString();

                int itpk = Sugar_helper.Db.Insertable<Models.Stu_group>(stu_Group).ExecuteReturnIdentity();

                sql = $"update student set group_id={itpk}, is_gleader=1 where stu_id={ints[i]}";

                Sugar_helper.Db.Ado.ExecuteCommand(sql);
            }


            for (int i = 0; i < group_count; i++)
            {
                int[] t_ids = new int[3];
                gen_rand(t_ids, teach_count);
                sql = string.Format("insert into g_data (g_id,c1,c2,c3) values({0}, {1}, {2}, {3})", i+1, t_ids[0], t_ids[1], t_ids[2]);
                System.Diagnostics.Debug.WriteLine(sql);
                //Thread.Sleep(50);
                Sugar_helper.Db.Ado.ExecuteCommand(sql);
            }

            StringBuilder sqlb = new StringBuilder();

            sqlb.Append("insert into t_data (t_id,");
            for (int j = 1; j <= teacher_choice; j++)
                sqlb.Append($"c{j},");
            sqlb.Remove(sqlb.Length-1, 1);
            sqlb.AppendLine(") values ( ");
            var sql_part1 = sqlb.ToString();

            for (int i = 0; i < teach_count; i++)
            {
                int[] g_ids = new int[8];
                gen_rand(g_ids, group_count);
                //sql = string.Format("insert into t_data (t_id,c1,c2,c3,c4,c5) values({0}, {1}, {2}, {3}, {4}, {5})", i+1, g_ids[0], g_ids[1], g_ids[2], g_ids[3], g_ids[4]);
                //sql = $"insert into t_data (t_id,c1,c2,c3,c4,c5,c7,c8,c) values({i+1}, {g_ids[0]}, {g_ids[0]}, {g_ids[0]}, {g_ids[0]}, {g_ids[0]})";
                sqlb.Clear();
                sqlb.Append(sql_part1);
                sqlb.Append($"{i + 1},");
                for (int j = 0; j < teacher_choice; j++)
                    sqlb.Append($"{g_ids[j]},");
                sqlb.Remove(sqlb.Length-1, 1);
                sqlb.AppendLine(")");

                sql = sqlb.ToString();

                System.Diagnostics.Debug.WriteLine(sql);
                //Thread.Sleep(50);
                Sugar_helper.Db.Ado.ExecuteCommand(sql);
            }

            MessageBox.Show("ok!");
        }


        void gen_rand(int[] ids, int ch_count)
        {
            for (int i = 0; i < ids.Length; i++) ids[i] = -1;

            Random rnd = new Random();
            int pos;

            for (int i = 0; i < ids.Length; i++)
            {
                pos = rnd.Next(1,ch_count);
                while (!check_rand(ids, pos))
                    pos = rnd.Next(ch_count);
                ids[i] = pos;
            }
        }

        bool check_rand(int[] ids, int id)
        {
            bool res = true;
            for (int i = 0; i < ids.Length; i++)
            {
                if (ids[i] == id)
                {
                    res = false;
                    break;
                }
            }

            return res;
        }

        List<int> GetRandomNumbers(int minValue, int maxValue, int count)
        {
            List<int> list = new List<int>();
            for (int i = 1;i<maxValue+1;i++) 
            {
                list.Add(i);
            }
            Shuffle(list);
            return list.Take(count).ToList();
        }

        void Shuffle(List<int> list)
        {
            Random rng = new Random();
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                int value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }

        void import_teacher(string teacher_file)
        {
            DataTable dt =  Tools.Read_excel.read_excel(teacher_file);
            if (dt == null) return;

            List<Teacher> teachers = new List<Teacher>();

            foreach (DataRow dr in dt.Rows)
            {
                Teacher t = new Teacher();
                t.teacher_uname = dr["用户名"].ToString();
                t.teacher_name = dr["姓名"].ToString();
                t.depart = dr["工作单位"].ToString();
                t.pwd = "123";
                t.info = dr["信息"].ToString();
                teachers.Add(t);
            }
            BLL.B_Teacher b_teacher = new BLL.B_Teacher();
            try
            {
                //int res = b_teacher.Add(teachers);
                int res = b_teacher.Add_custom(teachers);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void import_stu(string stu_file)
        {
            DataTable dt = Tools.Read_excel.read_excel(stu_file);
            if (dt == null) return;

            List<Student> students = new List<Student>();

            foreach (DataRow dr in dt.Rows)
            {
                Student stu = new Student();
                stu.grade = -1;
                stu.phone = dr["手机号码"].ToString();
                stu.stu_no = dr["学号"].ToString();
                stu.stu_name = dr["姓名"].ToString();
                stu.pwd = "123";
                stu.major_id = -1;
                stu.group_id = -1;
                students.Add(stu);
            }
            BLL.B_Student b_stu = new BLL.B_Student();
            try
            {
                var res = b_stu.Add(students);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {

            group_count = Convert.ToInt32(textBox1.Text);
            teach_count = Convert.ToInt32(textBox2.Text);
            student_count = Convert.ToInt32(textBox4.Text);
            teacher_choice = Convert.ToInt32(textBox3.Text);

            openFileDialog1.Multiselect = true;
            var teacher_file = "";
            var stu_file = "";
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;
            else
            {
                teacher_file = openFileDialog1.FileNames[0];
                stu_file = openFileDialog1.FileNames[1];
            }

            var res = Sugar_helper.Db.Ado.ExecuteCommand("truncate table Student; truncate table g_data;truncate table t_data;truncate table teacher; truncate table stu_group;");

            import_teacher(teacher_file);
            import_stu(stu_file);


            string sql = "";

            List<int> ints;
            ints = GetRandomNumbers(1, student_count, group_count);

            for (int i = 0; i < ints.Count; i++)
            {
                Models.Stu_group stu_Group = new Models.Stu_group();
                stu_Group.group_name = (i + 1).ToString();
                stu_Group.group_lead = ints[i];
                stu_Group.members = 1;
                stu_Group.project_info = (i + 1).ToString();
                stu_Group.project_name = (i + 1).ToString();

                int itpk = Sugar_helper.Db.Insertable<Models.Stu_group>(stu_Group).ExecuteReturnIdentity();

                sql = $"update student set group_id={itpk}, is_gleader=1 where stu_id={ints[i]}";

                Sugar_helper.Db.Ado.ExecuteCommand(sql);
            }


            for (int i = 0; i < group_count; i++)
            {
                int[] t_ids = new int[3];
                gen_rand(t_ids, teach_count);
                sql = string.Format("insert into g_data (g_id,c1,c2,c3) values({0}, {1}, {2}, {3})", i + 1, t_ids[0], t_ids[1], t_ids[2]);
                System.Diagnostics.Debug.WriteLine(sql);
                //Thread.Sleep(50);
                Sugar_helper.Db.Ado.ExecuteCommand(sql);
            }

            StringBuilder sqlb = new StringBuilder();

            sqlb.Append("insert into t_data (t_id,");
            for (int j = 1; j <= teacher_choice; j++)
                sqlb.Append($"c{j},");
            sqlb.Remove(sqlb.Length - 1, 1);
            sqlb.AppendLine(") values ( ");
            var sql_part1 = sqlb.ToString();

            for (int i = 0; i < teach_count; i++)
            {
                int[] g_ids = new int[8];
                gen_rand(g_ids, group_count);
                //sql = string.Format("insert into t_data (t_id,c1,c2,c3,c4,c5) values({0}, {1}, {2}, {3}, {4}, {5})", i+1, g_ids[0], g_ids[1], g_ids[2], g_ids[3], g_ids[4]);
                //sql = $"insert into t_data (t_id,c1,c2,c3,c4,c5,c7,c8,c) values({i+1}, {g_ids[0]}, {g_ids[0]}, {g_ids[0]}, {g_ids[0]}, {g_ids[0]})";
                sqlb.Clear();
                sqlb.Append(sql_part1);
                sqlb.Append($"{i + 1},");
                for (int j = 0; j < teacher_choice; j++)
                    sqlb.Append($"{g_ids[j]},");
                sqlb.Remove(sqlb.Length - 1, 1);
                sqlb.AppendLine(")");

                sql = sqlb.ToString();

                System.Diagnostics.Debug.WriteLine(sql);
                //Thread.Sleep(50);
                Sugar_helper.Db.Ado.ExecuteCommand(sql);
            }

            MessageBox.Show("ok!");
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            var res = Sugar_helper.Db.Ado.ExecuteCommand("truncate table Student; truncate table g_data;truncate table t_data;truncate table teacher; truncate table stu_group;");

        }
    }
}
